// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MatchForm.cs" company="Roman '4ux-nbIx' Novitsky">
//   Copyright (c) Roman '4ux-nbIx' Novitsky 2010
// </copyright>
// <summary>
//   Describes and manipulates the match form.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace MtgMC
{
    #region Namespace Imports

    using System;
    using System.Globalization;
    using System.Linq;
    using System.Windows.Forms;

    using DataModel;

    #endregion


    /// <summary>
    /// Describes and manipulates the match form.
    /// </summary>
    public partial class MatchForm : Form
    {
        #region Constants and Fields

        private readonly ApplicationData _applicationData;

        #endregion


        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="MatchForm"/> class.
        /// </summary>
        /// <param name="applicationData">
        /// The application data.
        /// </param>
        public MatchForm(ApplicationData applicationData)
        {
            Match = new Match();
            _applicationData = applicationData;
            InitializeComponent();

            DialogResult = DialogResult.OK;
        }

        #endregion


        #region Properties

        /// <summary>
        /// Gets a value indicating whether the ok button was clicked.
        /// </summary>
        /// <value>
        /// <c>True</c> if the ok button was clicked; otherwise, <c>false</c>.
        /// </value>
        public bool IsOkButtonClicked
        {
            get;
            private set;
        }


        /// <summary>
        /// Gets the match.
        /// </summary>
        /// <value>
        /// The match.
        /// </value>
        public Match Match
        {
            get;
            private set;
        }

        #endregion


        #region Methods

        /// <summary>
        /// Raises the <see cref="E:System.Windows.Forms.Form.Load"/> event.
        /// </summary>
        /// <param name="e">
        /// An <see cref="T:System.EventArgs"/> that contains the event data.
        /// </param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            DialogResult result = ShowMatchDetailsDialog();

            if (result != DialogResult.Cancel)
            {
                UpdateControls();

                return;
            }

            DialogResult = DialogResult.Cancel;
        }


        private void CancelMenuItemClick(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }


        private void DetailsMenuItemClick(object sender, EventArgs e)
        {
            DialogResult result = ShowMatchDetailsDialog();

            if (result == DialogResult.Cancel)
            {
                return;
            }
        }


        private void FinishMatchMenuItemClick(object sender, EventArgs e)
        {
            IsOkButtonClicked = true;

            DialogResult = DialogResult.OK;
        }


        private void NextRoundMenuItemClick(object sender, EventArgs e)
        {
            var roundForm = new MatchRoundForm(_applicationData.Player, Match.Opponent, Match.Rounds.Count + 1);

            DialogResult result = roundForm.ShowDialog();

            if (result == DialogResult.Cancel || !roundForm.IsOkButtonClicked)
            {
                return;
            }

            Match.Rounds.Add(roundForm.MatchRound);

            UpdateControls();
        }


        private DialogResult ShowMatchDetailsDialog()
        {
            var detailsForm = new MatchDetailsForm(Match, _applicationData);

            return detailsForm.ShowDialog();
        }


        private void UpdateControls()
        {
            Text = Match.Event.Name;
            _nextRoundMenuItem.Text = Match.Rounds.Any() ? "Next Round" : "Start Match";
            _eventNameLabel.Text = Match.Event.Name;
            _roundsPlayedLabel.Text = Match.Rounds.Count.ToString(CultureInfo.CurrentCulture);
            int wonRoundsCount = Match.Rounds.Where(r => r.Winner == _applicationData.Player).Count();
            int lostRoundsCount = Match.Rounds.Count - wonRoundsCount;
            _scoreLabel.Text = wonRoundsCount + "/" + lostRoundsCount;
            _startTimeLabel.Text = Match.StartTime.ToString("t", CultureInfo.CurrentCulture);
        }

        #endregion
    }
}